// ComposeEndGameDB.cpp : main project file.

#include "stdafx.h"

#define NO_PROGRESS

#include "..\EndGameDB\DatabaseRawT.h"
#include "..\EndGameDB\EndGameDb.h"
#include "..\EndGameDB\BuildDb.h"
#include "..\Engine\Engine.h"

// #include <vld.h>

using namespace System;
using namespace EndGameDB;


void Progress(int wm, int wk, int bm, int bk, int wmrank, int bmrank)
{
	Console::WriteLine("Building [{0},{1},{2},{3}]({4},{5})", wm, wk, bm, bk, wmrank, bmrank);
}

void CompressProgress(int wm, int wk, int bm, int bk, int wmrank, int bmrank)
{
	Console::WriteLine("Compressing [{0},{1},{2},{3}]({4},{5})", wm, wk, bm, bk, wmrank, bmrank);
}

void VerifyProgress(int wm, int wk, int bm, int bk, int wmrank, int bmrank)
{
	Console::WriteLine("Verifying [{0},{1},{2},{3}]({4},{5})", wm, wk, bm, bk, wmrank, bmrank);
}

void ValidateProgress(int wm, int wk, int bm, int bk, int wmrank, int bmrank)
{
	Console::WriteLine("Validating [{0},{1},{2},{3}]({4},{5})", wm, wk, bm, bk, wmrank, bmrank);
}


void ProgressMessage(wchar_t* sMessage)
{
	Console::WriteLine(gcnew System::String(sMessage));
}

void TimerPrintMessage(wchar_t* szMessage)
{
	Console::Write(gcnew System::String(szMessage));
}

int main(array<System::String ^> ^args)
{
	// configuration
	const int maxpiece = 7;
	int maxthreads = 1;

	// 32-bit
	const int maxRank = 7;
	const int bitsPerRow = 4;
	const int maxField = 31;

	// 64-bit
	//const int maxRank = 9;
	//const int bitsPerRow = 5;
	//const int maxField = 49;

	SetDatabaseRawDirectory(L"c:\\RawCzech");
	//SetDatabaseRawDirectory(L"c:\\RawEnglish");
	//SetDatabaseRawDirectory(L"c:\\RawInternational");

	SetDatabaseCprDirectory(L"c:\\Endgame\\Czech");
	const wchar_t* lpDir = L"c:\\Endgame\\Czech";
	//SetDatabaseCprDirectory(L"c:\\Endgame\\English");
	//const wchar_t* lpDir = L"c:\\Endgame\\English";
	//SetDatabaseCprDirectory(L"c:\\Endgame\\International");
	//const wchar_t* lpDir = L"c:\\Endgame\\International";

	Console::WriteLine("Process started...");

	InitEngine();
	InitEndGameDb<bitsPerRow, maxField>();
	InitCV();
	SetDbRawMemoryLimit(1024);


	OnBuildSubDbStartedPtr = Progress;
	OnCompressSubDbStartedPtr = CompressProgress;
	OnVerifyCompressSubDbStartedPtr = VerifyProgress;
	ProgressMessagePtr = ProgressMessage;
	OnValidationSubDbStartedPtr = ValidateProgress;


	
	omp_set_num_threads(maxthreads);

	// do building
	//BuildEndgameDatabase<BitBoard, maxField, bitsPerRow, maxRank>(maxpiece, eCheckersType::CzechCheckers);	
	//CompressDatabase<BitBoard, maxRank, bitsPerRow, maxField>(maxpiece, eCheckersType::CzechCheckers, CPR_ZIP, 64*1024);
	//VerifyDatabaseCompression<BitBoard, maxRank, maxField, bitsPerRow, maxpiece>(eCheckersType::CzechCheckers, lpDir);
	ValidateDatabase<BitBoard, maxRank, maxField, bitsPerRow, maxpiece>(eCheckersType::CzechCheckers, lpDir);

	//uint64 diskSpace = 0;
	//int dbcount = 0;
	//uint64 max;
	//dbcount = GetSubDbCount(8, &diskSpace, &max);
	//Console::WriteLine("DbCount: {0}, Disk space: {1} GB, MaxDbSize: {2} MB", 
	//	dbcount, diskSpace / (1024 * 1024 * 1024), max / (1024 * 1024));

	TimerPrint = TimerPrintMessage;
	PrintTimerOutput();
	LogTimerOutput("c:\\timer1.log");

	// clear subdatabases in memory
	UnloadAll();
	

	Console::WriteLine("Press Enter....");
	Console::ReadLine();
    return 0;
}
